package com.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web.model.User;
import com.web.service.ServiceDAOFactory;
import com.web.service.UserService;
import com.web.util.EmailCode;
import com.web.util.Md5Code;
import com.web.util.Sendmail;

@WebServlet("/regist.do")
public class RegistServlet extends HttpServlet{
	public static final String METHOO_REGIST_FORM = "0";
	public static final String METHOO_REGIST_STEP1 = "1";
	public static final String METHOO_REGIST_STEP2 = "2";
	private static final long serialVersionUID = 5774398684379587439L;
	
	@Override 
	protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		String method =req.getParameter("method");
		if(method !=null){
			switch(method){
			case RegistServlet.METHOO_REGIST_FORM:
				registTo(req,resp);
				break;
			case RegistServlet.METHOO_REGIST_STEP1:
				registStep1(req,resp);
				break;
			case RegistServlet.METHOO_REGIST_STEP2:
				registStep2(req,resp);
				break;
			default:
				break;
			}
			
		}
				
	}
	
	
	private void registStep1(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		User user=new User();
		try{
			UserService userService = ServiceDAOFactory.getUserService();
			user.setEmail(req.getParameter("email"));
			user.setNickname(req.getParameter("nickname"));
			//将密码加密
			String pwd  = Md5Code.createMd5Code(req.getParameter("password"));
			user.setPassword(pwd);
			user.setUserIntegral(10);
			user.setEmailVerify(false);
			//String emailCode = EmailCode.createEmailCode()+"-"+req.getParameter("email");
			String emailCode = "testtest";
			
			user.setEmailVerifyCode(emailCode);
			user.setLastLoginTime(System.currentTimeMillis());
			user.setLastLoginIp(req.getRemoteAddr());
			
			userService.save(user);
			req.getSession().setAttribute("s_user", user);
			//邮箱验证
			Sendmail s= new Sendmail();
//			s.sendMail(user.getEmail(), "验证码："+ emailCode +"-" + req.getParameter("email"), "淘书斋验证");
			s.sendMail(user.getEmail(), "验证码："+ emailCode +"-" + user.getId(), "淘书斋验证");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		req.getRequestDispatcher("/WEB-INF/view/user/verify_form.jsp").forward(req, resp);
	}
	
	private void registTo(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		req.getRequestDispatcher("/WEB-INF/view/user/register_form.jsp").forward(req, resp);
	}
	
	
	private void registStep2(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		//将用户输入的验证码解析,解析出用户Id和verifyCode
		String code = req.getParameter("code");
		String uid= EmailCode.getUid(code);
		if(uid==null){
			req.setAttribute("verify_err", "邮箱验证码错误，请检查后重输");
			req.getRequestDispatcher("/WEB-INF/view/user/register_form.jsp").forward(req, resp);
			return;
		}
		int id  = Integer.parseInt(uid);
		String vCode =  EmailCode.getUUID(code);
		//去数据库比较是否正确
		UserService userService = ServiceDAOFactory.getUserService();
		try{
			User u = userService.findById(id);
			//如果正确，将is_email_verify字段更新，跳转到regist_ok.jsp
			if(u != null && vCode.trim().equals(u.getEmailVerifyCode())){
				u.setEmailVerify(true);
				userService.update(u);
				req.getRequestDispatcher("/WEB-INF/view/user/register_ok.jsp").forward(req, resp);
				return;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
		doPost(req,resp);
	}
	
	
	
	
	

	
}
